package licode.unisop.platform.utils;

import cn.hutool.core.util.StrUtil;
import licode.unisop.platform.client.authinfo.AuPermitInfo;
import licode.unisop.platform.client.authinfo.AuPlatform;
import licode.unisop.platform.client.authinfo.AuRoleInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AuNormalizeUtil {

    /**
     * 根据权限分组和角色ID获取对应的权限码集合
     *
     * @param groups      权限分组
     * @param permitCodes 权限Id列表
     * @return 权限码列表
     */
    public static List<String> normalizePermitCodes(AuPlatform auPl,
                                                    List<String> groups,
                                                    List<String> permitCodes) {
        List<String> nlPermits = new ArrayList<>();

        nlOnlyPermits(auPl, nlPermits, permitCodes);

        if (null != groups) {
            for (String item : groups) {
                List<AuPermitInfo> grpList = auPl.getPermitGrp().get(item);
                if (null != grpList) {
                    nlOnlyPermits(auPl, nlPermits, grpList.stream()
                            .map(AuPermitInfo::getCode).collect(Collectors.toList()));
                }
            }
        }
        return nlPermits.stream().distinct().collect(Collectors.toList());
    }

    private static void nlOnlyPermits(AuPlatform auPl,
                                      List<String> nlPermits,
                                      List<String> permitCodes) {
        if (null != permitCodes) {
            for (String item : permitCodes) {
                AuPermitInfo rpItem = auPl.getPermitCodes().get(item);
                if (null != rpItem && StrUtil.isNotBlank(rpItem.getCode())) {
                    nlPermits.add(rpItem.getCode());
                }
            }
        }
    }

    /**
     * 根据角色分组和角色ID获取对应的角色码集合
     *
     * @param groups  角色分组
     * @param roleIds 角色Id列表
     * @return 角色码列表
     */
    public static List<String> normalizeRoleIds(AuPlatform auPl,
                                                List<String> groups,
                                                List<String> roleIds,
                                                List<String> computeIds) {
        List<String> roleCodes = new ArrayList<>();

        computeRoleIds(auPl, roleIds, roleCodes, computeIds);

        if (null != groups) {
            for (String item : groups) {
                List<AuRoleInfo> grpList = auPl.getRoleGrp().get(item);
                if (null != grpList) {
                    computeRoleIds(auPl, grpList.stream()
                            .map(AuRoleInfo::getId).collect(Collectors.toList()),
                            roleCodes, computeIds);
                }
            }
        }
        return roleCodes.stream().distinct().collect(Collectors.toList());
    }

    private static void computeRoleIds(AuPlatform auPl,
                                       List<String> roleIds,
                                       List<String> roleCodes,
                                       List<String> computeIds) {
        if (null != roleIds) {
            for (String item : roleIds) {
                AuRoleInfo rpItem = auPl.getRoleIds().get(item);
                if (null != rpItem) {
                    computeIds.add(rpItem.getId());
                    if (StrUtil.isNotBlank(rpItem.getCode())) {
                        roleCodes.add(rpItem.getCode());
                    }
                    if (null != rpItem.getChildren()) {
                        roleCodes.addAll(rpItem.getChildren().stream()
                                .map(AuRoleInfo::getCode)
                                .filter(StrUtil::isNotBlank).collect(Collectors.toList()));
                        computeIds.addAll(rpItem.getChildren().stream()
                                .map(AuRoleInfo::getId).collect(Collectors.toList()));
                    }
                }
            }
        }
    }
}
